package com.tulingxueyuan.mall.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.dto.PmsProCateWithChirendDTO;
import com.tulingxueyuan.mall.dto.PmsProductCategoryDTO;
import com.tulingxueyuan.mall.dto.ProductCategoryDTO;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductAttributeCategory;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.mapper.PmsProductCategoryMapper;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategoryAttributeRelation;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductCategoryAttributeRelationService;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author XuShu
 * @since 2023-05-08
 */
@Service
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory> implements PmsProductCategoryService {
    @Resource
    private PmsProductCategoryMapper pmsProductCategoryMapper;
    @Resource
    private PmsProductCategoryAttributeRelationService proCateAttrRelationService;
    @Resource
    private PmsProductCategoryAttributeRelationService pmsProductCategoryAttributeRelationService;
    /**
     * 查询分类列表
     * @param pageNum
     * @param pageSize
     * @param paraId
     * @return
     */
    @Override
    public Page listPage(Integer pageNum, Integer pageSize, Integer paraId) {
        Page<PmsProductCategory> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getParentId,paraId);
        this.page(pageInfo,wrapper);
        return pageInfo;
    }

    /**
     * 修改导航栏状态
     * @param ids
     * @param navStatus
     * @return
     */
    @Override
    public boolean updateNavStatus(Integer[] ids, Integer navStatus) {
        LambdaUpdateWrapper<PmsProductCategory> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(PmsProductCategory::getNavStatus,navStatus)
                .in(PmsProductCategory::getId,ids);
        return this.update(wrapper);
    }

    @Override
    public boolean updateShowStatus(Integer[] ids, Integer showStatus) {
        LambdaUpdateWrapper<PmsProductCategory> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(PmsProductCategory::getShowStatus,showStatus)
                .in(PmsProductCategory::getId,ids);
        return this.update(wrapper);
    }

    @Transactional
    @Override
    public boolean saveWithAttrreLation(ProductCategoryDTO productCategoryDTO) {
        boolean isSave,isSaveAttrbute;
//        先添加分类
        PmsProductCategory pmsProductCategory = new PmsProductCategory();
//        使用spring提供的拷贝方法
        BeanUtils.copyProperties(productCategoryDTO,pmsProductCategory);
//        设置默认值
        pmsProductCategory.setProductCount(0);
//        设置级联级别
        if (productCategoryDTO.getParentId() == 0){
            pmsProductCategory.setLevel(0);
        }else {
//            根据上级分类id查询上级
            LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PmsProductCategory::getParentId,pmsProductCategory.getParentId());
            PmsProductCategory category = getOne(wrapper);
            pmsProductCategory.setLevel(category.getLevel()+1);
        }
        isSave = this.save(pmsProductCategory);
//        再添加分类和属性的关联表
//        遍历添加到集合中
        List<PmsProductCategoryAttributeRelation> proCateAttrRelationList = new ArrayList<>();
        for (Long attrbuteId : productCategoryDTO.getProductAttributeIdList()) {
            PmsProductCategoryAttributeRelation proCateAttrRelation = new PmsProductCategoryAttributeRelation();
//            设置默认值
            proCateAttrRelation.setProductAttributeId(attrbuteId);
            proCateAttrRelation.setProductCategoryId(pmsProductCategory.getId());
            proCateAttrRelationList.add(proCateAttrRelation);
        }
//        最后执行添加
        isSaveAttrbute = proCateAttrRelationService.saveBatch(proCateAttrRelationList);
        return isSave && isSaveAttrbute;
    }

    @Transactional
    @Override
    public boolean editWithAttrbibute(PmsProductCategoryDTO pmsProductCategoryDTO) {

        boolean isEdit,isEditWithAttrbute;
//        先删除再添加
            PmsProductCategory pmsProductCategory = new PmsProductCategory();
//            使用spring自带拷贝工具
            BeanUtils.copyProperties(pmsProductCategoryDTO,pmsProductCategory);
//            执行修改
             isEdit = this.updateById(pmsProductCategory);

//        修改完修改商品分类属性
        List<Long> productAttributeIdList = pmsProductCategoryDTO.getProductAttributeIdList();
        LambdaUpdateWrapper<PmsProductCategoryAttributeRelation> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PmsProductCategoryAttributeRelation::getProductCategoryId,pmsProductCategoryDTO.getId());
        pmsProductCategoryAttributeRelationService.remove(wrapper);
        List<PmsProductCategoryAttributeRelation> relationList = new ArrayList<>();
        for (Long aid : productAttributeIdList) {
            PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation();
            relation.setProductAttributeId(aid);
            relation.setProductCategoryId(pmsProductCategoryDTO.getId());
            relationList.add(relation);
        }
//        先删除再添加
        isEditWithAttrbute = pmsProductCategoryAttributeRelationService.saveBatch(relationList);
        return isEditWithAttrbute && isEdit;
    }

    @Override
    public List<PmsProCateWithChirendDTO> getWithChildrenList() {
        List<PmsProCateWithChirendDTO> list =  pmsProductCategoryMapper.getWithChildrenList();
        return list;
    }
}
